home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / misc / gnuplot-3.7src.lha / gnuplot-3.7src / gnuplot-3.7.lha / gnuplot-3.7 / docs / doc2ipf.c < prev    next >
C/C++ Source or Header  |  1998-10-19  |  12KB  |  465 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: doc2ipf.c,v 1.20 1998/04/14 00:16:59 drd Exp $";
  3. #endif
  4.  
  5. /* GNUPLOT - doc2ipf.c */
  6.  
  7. /*[
  8.  * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted,
  12.  * provided that the above copyright notice appear in all copies and
  13.  * that both that copyright notice and this permission notice appear
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the complete modified source code.  Modifications are to
  18.  * be distributed as patches to the released version.  Permission to
  19.  * distribute binaries produced by compiling modified sources is granted,
  20.  * provided you
  21.  *   1. distribute the corresponding source modifications from the
  22.  *    released version in the form of a patch file along with the binaries,
  23.  *   2. add special version identification to distinguish your version
  24.  *    in addition to the base release version number,
  25.  *   3. provide your name and address as the primary contact for the
  26.  *    support of your modified version, and
  27.  *   4. retain our contact information in regard to use of the base
  28.  *    software.
  29.  * Permission to distribute the released version of the source code along
  30.  * with corresponding source modifications in the form of a patch file is
  31.  * granted with same provisions 2 through 4 for binary distributions.
  32.  *
  33.  * This software is provided "as is" without express or implied warranty
  34.  * to the extent permitted by applicable law.
  35. ]*/
  36.  
  37. /*
  38.  * doc2ipf.c  -- program to convert Gnuplot .DOC format to OS/2
  39.  * ipfc  (.inf/.hlp) format.
  40.  *
  41.  * Modified by Roger Fearick from doc2rtf by M Castro 
  42.  *
  43.  * usage:  doc2ipf gnuplot.doc gnuplot.itl
  44.  *
  45.  */
  46.  
  47. /* note that tables must begin in at least the second column to */
  48. /* be formatted correctly and tabs are forbidden */
  49.  
  50. #ifdef HAVE_CONFIG_H
  51. # include "config.h"
  52. #endif
  53.  
  54. #include "ansichek.h"
  55. #include "stdfn.h"
  56.  
  57. #define MAX_LINE_LEN 1023
  58.  
  59. #include "doc2x.h"
  60. #include "xref.h"
  61.  
  62. #define MAX_COL 6
  63.  
  64. /* From xref.c */
  65. extern void *xmalloc __PROTO((size_t));
  66.  
  67. void convert __PROTO((FILE *, FILE *));
  68. void process_line __PROTO((char *, FILE *));
  69.  
  70. /* malloc's are not being checked ! */
  71.  
  72. struct TABENTRY {        /* may have MAX_COL column tables */
  73.     struct TABENTRY *next;
  74.     char col[MAX_COL][256];
  75. };
  76.  
  77. struct TABENTRY table = { NULL };
  78. struct TABENTRY *tableins = &table;
  79. int tablecols = 0;
  80. int tablewidth[MAX_COL] = {0, 0, 0, 0, 0, 0};    /* there must be the correct */
  81. int tablelines = 0;        /* number of zeroes here */
  82.  
  83. static boolean debug = FALSE;
  84.  
  85.  
  86. int main(argc, argv)
  87. int argc;
  88. char **argv;
  89. {
  90.     FILE *infile;
  91.     FILE *outfile;
  92.     if (argc == 4 && argv[3][0] == '-' && argv[3][1] == 'd')
  93.     debug = TRUE;
  94.  
  95.     if (argc != 3 && !debug) {
  96.     fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
  97.     exit(EXIT_FAILURE);
  98.     }
  99.     if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
  100.     fprintf(stderr, "%s: Can't open %s for reading\n",
  101.         argv[0], argv[1]);
  102.     exit(EXIT_FAILURE);
  103.     }
  104.     if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
  105.     fprintf(stderr, "%s: Can't open %s for writing\n",
  106.         argv[0], argv[2]);
  107.     fclose(infile);
  108.     exit(EXIT_FAILURE);
  109.     }
  110.     parse(infile);
  111.     convert(infile, outfile);
  112.     exit(EXIT_SUCCESS);
  113. }
  114.  
  115. void convert(a, b)
  116. FILE *a, *b;
  117. {
  118.     static char line[MAX_LINE_LEN+1];
  119.  
  120.     /* generate ipf header */
  121.     fprintf(b, ":userdoc.\n:prolog.\n");
  122.     fprintf(b, ":title.GNUPLOT\n");
  123.     fprintf(b, ":docprof toc=12345.\n:eprolog.\n");
  124.  
  125.     /* process each line of the file */
  126.     while (get_line(line, sizeof(line), a)) {
  127.     process_line(line, b);
  128.     }
  129.  
  130.     /* close final page and generate trailer */
  131.     fprintf(b, "\n:euserdoc.\n");
  132.  
  133.     list_free();
  134. }
  135.  
  136. void process_line(line, b)
  137. char *line;
  138. FILE *b;
  139. {
  140.     static int line_count = 0;
  141.     static char line2[MAX_LINE_LEN+1];
  142.     static int last_line;
  143.     char hyplink1[64];
  144.     char *pt, *tablerow;
  145.     int i;
  146.     int j;
  147.     static int startpage = 1;
  148.     char str[MAX_LINE_LEN+1];
  149.     char topic[MAX_LINE_LEN+1];
  150.     int k, l;
  151.     static int tabl = 0;
  152.     static int para = 0;
  153.     static int inquote = FALSE;
  154.     static int inref = FALSE;
  155.     static int intable = FALSE;
  156.     static int intablebut = FALSE;
  157.     static int introffheader = FALSE;
  158.     static char tablechar = '@';
  159.     static FILE *bo = NULL, *bt = NULL;
  160.     static char tabledelim[4] = "%@\n";
  161.     static int nblanks = 0;
  162.     struct LIST *klist;
  163.  
  164.     line_count++;
  165.  
  166.     if (introffheader)
  167.     fprintf(stderr, "%s\n", line);
  168.     if (bo == NULL)
  169.     bo = b;
  170.     i = 0;
  171.     j = 0;
  172.     nblanks = 0;
  173.     while (line[nblanks] == ' ')
  174.     ++nblanks;
  175.     while (line[i] != NUL) {
  176.     if (introffheader) {
  177.         if (line[i] != '\n')
  178.         line2[j] = line[i];
  179.         else
  180.         line2[j] = NUL;
  181.     } else
  182.         switch (line[i]) {
  183.         case '$':
  184.         if (intable && (tablechar != '$') && (line[0] == '%')) {
  185.             ++i;
  186.             if (line[i + 1] == '$' || line[i] == 'x' || line[i] == '|') {
  187.             while (line[i] != '$')
  188.                 line2[j++] = line[i++];
  189.             --j;
  190.             } else {
  191.             while (line[i] != '$')
  192.                 i++;
  193.             if (line[i + 1] == ',')
  194.                 i++;
  195.             if (line[i + 1] == ' ')
  196.                 i++;
  197.             line2[j] = line[++i];
  198.             }
  199.         } else
  200.             line2[j] = line[i];
  201.         break;
  202.         case ':':
  203.         strcpy(&line2[j], "&colon.");
  204.         j += strlen("&colon.") - 1;
  205.         break;
  206.  
  207.         case '&':
  208.         /* real hack to solve \&_ in postscript doc tables */
  209.         /* (which are a special case hack anyway. */
  210.         if (j > 0 && line2[j - 1] == '\\') {
  211.             j -= 2;
  212.             break;
  213.         }
  214.         strcpy(&line2[j], "&.");
  215.         j += strlen("&.") - 1;
  216.         break;
  217.  
  218.         case '\r':
  219.         case '\n':
  220.         break;
  221.         case '`':        /* backquotes mean boldface or link */
  222.         if (nblanks > 7) {
  223.             line2[j] = line[i];
  224.             break;
  225.         }
  226.         if ((!inref) && (!inquote)) {
  227.             k = i + 1;    /* index into current string */
  228.             l = 0;    /* index into topic string */
  229.             while ((line[k] != '`') && (line[k] != 0)) {
  230.             topic[l] = line[k];
  231.             k++;
  232.             l++;
  233.             }
  234.             topic[l] = 0;
  235.             klist = lookup(topic);
  236.             if (klist != NULL && (k = klist->line) > 0) {
  237.             sprintf(hyplink1, ":link reftype=hd res=%d.", k);
  238.             strcpy(line2 + j, hyplink1);
  239.             j += strlen(hyplink1) - 1;
  240.  
  241.             inref = k;
  242.             } else {
  243.             if (debug)
  244.                 fprintf(stderr, "Can't make link for \042%s\042 on line %d\n", topic, line_count);
  245.             strcpy(line2 + j, ":hp2.");
  246.             j += 4;
  247.             inquote = TRUE;
  248.             }
  249.         } else {
  250.             if (inquote && inref)
  251.             fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
  252.             if (inquote) {
  253.             strcpy(line2 + j, ":ehp2.");
  254.             j += 5;
  255.             inquote = FALSE;
  256.             }
  257.             if (inref) {
  258.             /* must be inref */
  259.             strcpy(line2 + j, ":elink.");
  260.             j += 6;
  261.             inref = FALSE;
  262.             }
  263.         }
  264.         break;
  265.         default:
  266.         line2[j] = line[i];
  267.         }
  268.     i++;
  269.     j++;
  270.     if ((j >= sizeof(line2))) {
  271.         fprintf(stderr, "MAX_LINE_LEN exceeded\n");
  272.         if (inref || inquote)
  273.         fprintf(stderr, "Possible missing link character (`) near above line number\n");
  274.         abort();
  275.     }
  276.     line2[j] = NUL;
  277.     }
  278.  
  279.     i = 1;
  280.  
  281.     switch (line[0]) {        /* control character */
  282.     case '?':{            /* interactive help entry */
  283.         if (intable)
  284.         intablebut = TRUE;
  285.         break;
  286.     }
  287.     case '@':{            /* start/end table */
  288.         intable = !intable;
  289.         if (intable) {
  290.         tablechar = '@';
  291.         introffheader = FALSE;
  292.         tablelines = 0;
  293.         tablecols = 0;
  294.         tableins = &table;
  295.         for (j = 0; j < MAX_COL; j++)
  296.             tablewidth[j] = 0;
  297.         } else {        /* dump table */
  298.         int header = 0;
  299.         intablebut = FALSE;
  300.         tableins = &table;
  301.         fprintf(b, ":table cols=\'");
  302.         for (j = 0; j < MAX_COL; j++)
  303.             if (tablewidth[j] > 0)
  304.             fprintf(b, " %d", tablewidth[j]);
  305.         fprintf(b, "\'.\n");
  306.         tableins = tableins->next;
  307.         if (tableins->next != NULL)
  308.             header = (tableins->next->col[0][0] == '_');
  309.         if (header)
  310.             tableins->next = tableins->next->next;
  311.         while (tableins != NULL) {
  312.             fprintf(b, ":row.\n");
  313.             for (j = 0; j < tablecols; j++)
  314.             if (header)
  315.                 fprintf(b, ":c.:hp9.%s:ehp9.\n", tableins->col[j]);
  316.             else
  317.                 fprintf(b, ":c.%s\n", tableins->col[j]);
  318.             tableins = tableins->next;
  319.             header = 0;
  320.         }
  321.         fprintf(b, ":etable.\n");
  322.         if (bt != NULL) {
  323.             rewind(bt);
  324.             while (get_line(str, sizeof(str), bt))
  325.             fputs(str, b);
  326.             fclose(bt);
  327.             remove("doc2ipf.tmp");
  328.             bt = NULL;
  329.             bo = b;
  330.         }
  331.         }
  332.         break;
  333.     }
  334.     case '#':{            /* latex table entry */
  335.         break;        /* ignore */
  336.     }
  337.     case '%':{            /* troff table entry */
  338.         if (intable) {
  339.         if (introffheader) {
  340.             fprintf(stderr, ">%s\n", line2);
  341.             fprintf(stderr, "tablechar: %c\n", tablechar);
  342.             if (line2[1] == '.')
  343.             break;
  344.             pt = strchr(line2, '(');
  345.             if (pt != NULL)
  346.             tablechar = *(pt + 1);
  347.             fprintf(stderr, "tablechar: %c\n", tablechar);
  348.             pt = strchr(line2 + 2, '.');
  349.             if (pt != NULL)
  350.             introffheader = FALSE;
  351.             break;
  352.         }
  353.         if (line[1] == '.') {    /* ignore troff commands */
  354.             introffheader = TRUE;
  355.             break;
  356.         }
  357.         tablerow = line2;
  358.         tableins->next = xmalloc(sizeof(struct TABENTRY));
  359.         tableins = tableins->next;
  360.         tableins->next = NULL;
  361.         j = 0;
  362.         tabledelim[1] = tablechar;
  363.         line2[0] = tablechar;
  364.         while ((pt = strtok(tablerow, tabledelim + 1)) != NULL) {
  365.             if (*pt != NUL) {    /* ignore null columns */
  366.             /* this fails on format line */
  367.             assert(j < MAX_COL);
  368.             strcpy(tableins->col[j], pt);
  369.             k = strlen(pt);
  370.             if (k > tablewidth[j])
  371.                 tablewidth[j] = k;
  372.             ++j;
  373.             tablerow = NULL;
  374.             if (j > tablecols)
  375.                 tablecols = j;
  376.             }
  377.         }
  378.         while (j < MAX_COL)
  379.             tableins->col[j++][0] = NUL;
  380.         }
  381.         break;        /* ignore */
  382.     }
  383.     case '\n':            /* empty text line */
  384.     para = 0;
  385.     tabl = 0;
  386.     fprintf(bo, ":p.");
  387.     break;
  388.     case ' ':{            /* normal text line */
  389.         if (intable && !intablebut)
  390.         break;
  391.         if (intablebut) {    /* indexed items in  table, copy
  392.                    to file after table by saving in
  393.                    a temp file meantime */
  394.         if (bt == NULL) {
  395.             fflush(bo);
  396.             bt = fopen("doc2ipf.tmp", "w+");
  397.             if (bt == NULL)
  398.             fprintf(stderr, "cant open temp\n");
  399.             else
  400.             bo = bt;
  401.         }
  402.         }
  403.         if (intablebut && (bt == NULL))
  404.         break;
  405.         if ((line2[1] == 0) || (line2[1] == '\n')) {
  406.         fprintf(bo, ":p.");
  407.         para = 0;
  408.         }
  409.         if (line2[1] == ' ') {
  410.         if (!tabl)
  411.             fprintf(bo, ":p.");
  412.         fprintf(bo, "%s", &line2[1]);
  413.         fprintf(bo, "\n.br\n");
  414.         tabl = 1;
  415.         para = 0;
  416.         } else {
  417.         if (!para) {
  418.             para = 1;    /* not in para so start one */
  419.             tabl = 0;
  420.         }
  421.         fprintf(bo, "%s \n", &line2[1]);
  422.         }
  423.         fflush(bo);
  424.         break;
  425.     }
  426.     case '^':
  427.     break;            /* ignore */
  428.     default:{
  429.         if (isdigit((int)line[0])) {    /* start of section */
  430.         if (intable) {
  431.             intablebut = TRUE;
  432.             if (bt == NULL) {
  433.             fflush(bo);
  434.             bt = fopen("doc2ipf.tmp", "w+");
  435.             if (bt == NULL)
  436.                 fprintf(stderr, "cant open temp\n");
  437.             else
  438.                 bo = bt;
  439.             }
  440.         }
  441.         if (startpage)    /* use new level 0 item */
  442.             refs(0, bo, NULL, NULL, NULL);
  443.         else
  444.             refs(last_line, bo, NULL, NULL, NULL);
  445.         para = 0;    /* not in a paragraph */
  446.         tabl = 0;
  447.         last_line = line_count;
  448.         startpage = 0;
  449.         fprintf(stderr, "%d: %s\n", line_count, &line2[1]);
  450.         klist = lookup(&line2[2]);
  451.         if (klist != NULL)
  452.             k = klist->line;
  453.         /*if( k<0 ) fprintf(bo,":h%c.", line[0]=='1'?line[0]:line[0]-1);
  454.            else */
  455.         fprintf(bo, ":h%c res=%d.", line[0], line_count);
  456.         fprintf(bo, &(line2[1]));    /* title */
  457.         fprintf(bo, "\n:p.");
  458.         } else
  459.         fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
  460.             line[0], line_count);
  461.     }
  462.     break;
  463.     }
  464. }
  465.